using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Autobot
{
    class Goal : IObjectInterface
    {
        private Vector2 virtualPosition;            // The destination of the robot
        private Vector2 realPosition;
        private float distanceThreshold = 0.05f;    //0.05m 5 cm
        
        //Drawing stuff for Goal
        Texture2D sprite;
        SpriteBatch goalBatch;

        public Goal(Vector2 VirtualPosition, Vector2 RealPosition)
        {
            virtualPosition = new Vector2(VirtualPosition.X,VirtualPosition.Y);
            realPosition = new Vector2(RealPosition.X,RealPosition.Y); 
        }

        // I am forcing that we change both at the same time so we don't forget about managing both
        public void SetPosition(Vector2 VirtualPosition, Vector2 RealPosition)
        {
            virtualPosition = new Vector2(VirtualPosition.X,VirtualPosition.Y);
            realPosition = new Vector2(RealPosition.X,RealPosition.Y); 
        }
        public Vector2 GetRealPosition()
        {
            return realPosition;
        }
        public Vector2 GetVirtualPosition()
        {
            return virtualPosition;
        }
        public bool CheckGoalReached(Vector2 RealPos)
        {
            Vector2 Diff = RealPos - realPosition;
            if (Diff.Length() <= distanceThreshold)
                return true;
            return false;
        }

        public void Draw()
        {
            //Set up the drawing rectangle
            Rectangle goalDrawRect = new Rectangle((int)(virtualPosition.X - sprite.Width / 2), (int)(virtualPosition.Y - sprite.Height / 2), sprite.Width, sprite.Height);
            goalBatch.Begin(SpriteBlendMode.AlphaBlend);
            goalBatch.Draw(sprite, goalDrawRect, Color.White);
            goalBatch.End();

        }
        public void Update(GameTime gameTime, Vector2 RealRobotPos)
        {
            if (CheckGoalReached(RealRobotPos))
            {
                Popup.Show("Target Reached! HOORAY!");
                // GOAL HAS BEEN REACHED. What now?
            }
        }
        public void LoadContent(ContentManager Content, GraphicsDevice drawDevice)
        {
            //Initialize the car's sprite batch
            goalBatch = new SpriteBatch(drawDevice);

            // Remember the graphics set to the robot
            sprite = Content.Load<Texture2D>("Destination");
        }
        public void Dispose()
        {
            // Dispose of the robot sprite if it hasn't been disposed of yet
            if (!sprite.IsDisposed)
            {
                sprite.Dispose();
            }

            //Dispose of the sprite batches
            if (!goalBatch.IsDisposed)
                goalBatch.Dispose();
        }

    }
}
